<?php
class State_model extends MY_Model
{
	public
		$table = 'states',
		$prKey = 'state_ID'
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(

			'state_ID' 					=> T_('Область'),
			'state_name' 				=> T_('State'),
			'state_eng_name' 			=> T_('State'),
			'state_declension' 			=> '',
			'state_center_declension' 	=> '',
			'state_capital_name' 		=> T_('Областной центр')
		);

		$this->rules = array(
            
			'state_name' 				=> "trim|strip_tags|max_length[64]",
			'state_eng_name' 			=> "trim|strip_tags|max_length[64]",
			'state_declension' 			=> "trim|strip_tags|max_length[64]",
			'state_center_declension' 	=> "trim|strip_tags|max_length[64]",
			'state_capital_name' 		=> "trim|strip_tags|max_length[64]",
		);

	}

	function getSelectOptions()
	{
		$this->db->where('lang_id', 2); // Russian lang
		$this->db->order_by('state_name');
		$query = $this->db->get($this->table);
		$options = array('0' => T_('Выберите область'));

		foreach ($query->result() as $row)
		{
			$options[$row->state_ID] = $row->state_name;
		}

		return $options;
	}

	function getCitiesSelectOptions($state_ID = null)
	{
		if (!empty($state_ID))
		{
			$this->db->where('city_state_ID', $state_ID);
		}

		$this->db->order_by('city_name');
		$query = $this->db->get($this->tbl_cities);

		if ($query->num_rows() > 0)
		{
			$options[0] = array('0' => T_('Выберите город'));
			foreach ($query->result() as $row)
			{
				$options[$row->city_ID] = $row->city_name;
			}
		}
		else
		{
			return array('0' => T_("Пустой список"));
		}

		return $options;
	}












	function hasHotels($city_ID = '')
	{
		$this->db->select('COUNT(hotel_ID) AS num_hotels');
		$this->db->from('hotels, cities');
		$this->db->where('`hotel_city_ID`=`city_ID`');
		$this->db->where('city_ID', $city_ID);
		$result = $this->db->get()->row();

		return $result->num_hotels;
	}

	function search($config = array())
	{
		$query = $this->db->get($this->geo_city_model->table);

		$config = array_merge(array('rowsOnPage' => 5, 'order' => 'city_ID'), $config);

		$country_ID = $this->input->get_post('country_ID');

		$region_ID = $this->input->get_post('region_ID');

		if ($this->input->post('per_page'))
		{
			$config['rowsOnPage'] = $this->input->post('per_page');
			$per_page = 0;
		}
		else if ($this->input->get('per_page'))
		{
			$per_page = $this->input->get('per_page');
		}
		else
		{
			$per_page = 0;
		}

		$order = $this->input->get_post('order');
		$direction = $this->input->get_post('direction');
		$search = $this->input->get_post('search');

		// main sql
		$sql = '';
		$sql .= ' FROM cities c';
		$sql .= ' LEFT JOIN regions r ON r.region_ID=c.city_region_ID';
		$sql .= ' WHERE 1 = 1';

		if ($country_ID)
		{
			$sql .= ' AND c.city_region_ID = ' . $this->db->escape($region_ID);
		}

		if ($search)
		{
			$sql .= ' AND c.city_name LIKE ' . $this->db->escape('%' . $search . '%');
		}

		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by c.city_created';
				break;

			default:
				$sql .= ' ORDER by c.city_ID';
				$direction = 'DESC';
		}

		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		// pagination config
		$config['base_url'] = Xurl_add(Xregenerate_url('per_page,search', true), 'search', $search);
		$config['per_page'] = $config['rowsOnPage'];
		$config['total_rows'] = $this->getRowCount($sql);

		$this->pagination->initialize($config);

		// main sql
		$sql = 'SELECT *' . $sql . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']);

		$this->data['query'] = $this->db->query($sql);
		$this->data['per_page'] = $config['per_page'];
		$this->data['search'] = $search;
		$this->data['country_ID'] = $country_ID;
		$this->data['region_ID'] = $region_ID;
	}

	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);

		$error = "";
		$report = "";
		foreach ($items as $id)
		{
			if (!$this->geo_city_model->hasHotels($id) > 0)
			{
				$res = $this->db->delete($this->table, array($this->prKey => $id));
				$report .= ",1"; // deleted

				if (is_null($res))
				{
					$error = true;
				}
			}
			else
			{
				$report .= ",0"; // not deleted
			}
		}
		return array("error" => $error, "report" => substr($report, 1));
	}
}
?>